package com.ming.controller;

import com.lly835.bestpay.model.PayResponse;
import com.ming.dto.OrderDTO;
import com.ming.enums.ResultEnum;
import com.ming.exception.SellException;
import com.ming.service.OrderService;
import com.ming.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**支付
 * Created by LIANG on 2018/3/25.
 */
@Controller
@RequestMapping("/templates/pay")
@Slf4j
public class PayController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private PayService payService;
    @RequestMapping("/create")
    public ModelAndView create(@RequestParam("orderId")String orderId,
                               @RequestParam("returnUrl")String returnUrl,
                               Map<String,Object> map){
        //查询订单
        OrderDTO orderDTO = orderService.findOne(orderId);
        if (orderDTO ==null){
            throw new SellException(ResultEnum.ORDER_DETAIL_EMPTY);
        }
        //发起支付
        PayResponse  payResponse = payService.create(orderDTO);
        map.put("payResponse",payResponse);
        map.put("returnUrl",returnUrl);
        return new ModelAndView("templates/pay/create",map);
    }
    /**
     * 微信异步通知
     * @param notifyData
     * @return
     */
    @RequestMapping("/notify")
    public ModelAndView notify(String notifyData){
        payService.asyncNotify(notifyData);
        return new ModelAndView("templates/pay/success");
    }
}
